package csl.mq.rabbitmq;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.UUID;

/**
 * @Description: TODO
 * @author: 隔山海
 * @date: 2021年09月05日 10:53
 */
@SpringBootTest
@Slf4j
public class NormalSendMessage {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送简单消息
     */
    @Test
    public void sendMessage() throws IOException {
        rabbitTemplate.convertAndSend("hello.exchange","info", DateUtil.now(),message -> {
            String messageId = UUID.randomUUID().toString().replace("-", "");
            message.getMessageProperties().setMessageId(messageId);
            return message;
        });
        System.err.println("消息发送成功！");
        System.in.read();
    }

    /**
     * 发送延迟消息
     */
    @Test
    public void sendDeadMessage() throws IOException {
        System.out.println("发送时间"+DateUtil.now());
        rabbitTemplate.convertAndSend("dealy", "我是延迟消息"
                , message -> {
                    String messageId = UUID.randomUUID().toString().replace("-", "");
                    message.getMessageProperties().setMessageId(messageId);
                    message.getMessageProperties().setExpiration("5000"); //消息存活5秒
                return message;
            }
        );
        System.err.println("消息发送成功！");
        System.in.read();
    }
}
